#include "siprtsp.h"
#include <osipparser2/osip_port.h>
#include <stdlib.h>
#include <string.h>
#define END_LINE "\r\n"

mk_sip_rtsp *mk_sip_rtsp_init()
{
    return (mk_sip_rtsp*)osip_malloc(sizeof(mk_sip_rtsp));
}

void mk_sip_rtsp_uninit(mk_sip_rtsp *rtspcontrol)
{
    if (!rtspcontrol)
        return;
    if (rtspcontrol->method)
        osip_free(rtspcontrol->method);
    if (rtspcontrol->range)
        osip_free(rtspcontrol->range);
    if (rtspcontrol->version)
        osip_free(rtspcontrol->version);
    if (rtspcontrol->scale)
        osip_free(rtspcontrol->scale);
    osip_free(rtspcontrol);
}

void mk_siprtsp_parse(char *rtspbuf, mk_sip_rtsp* rtspcontrol)
{
    char tempbuf[255] = { 0 };
    char *method;
    char *scale;
    char *scalend;
    char *range;
    char *rangend;


    method = strchr(rtspbuf, ' ');
    if(!method || method - rtspbuf >= sizeof(tempbuf))
        return;
    memcpy(tempbuf, rtspbuf, method - rtspbuf);
    rtspcontrol->method = osip_strdup(tempbuf);
    memset(tempbuf, 0, sizeof(tempbuf));
    if (!osip_strcasecmp(rtspcontrol->method, "PAUSE"))
    {
        range = strstr(rtspbuf, "PauseTime: ") + strlen("PauseTime: ");
        if (*range)
        {
            rangend = strstr(range, END_LINE);
            if(!rangend || rangend - range >= sizeof(tempbuf))
                return;
            memcpy(tempbuf, range, rangend - range);
            rtspcontrol->range = osip_strdup(tempbuf);
        }

        return;
    }

    scale = strstr(rtspbuf, "Scale: ");
    if(!scale)
        return;
    scale += strlen("Scale: ");

    scalend = strstr(scale, END_LINE);
    if(!scalend || scalend - scale >= sizeof(tempbuf))
        return;
    memcpy(tempbuf, scale, scalend - scale);
    rtspcontrol->scale = osip_strdup(tempbuf);
    memset(tempbuf, 0, sizeof(tempbuf));

    range = strstr(rtspbuf, "Range: npt=");
    if(!range)
        return;
    range += strlen("Range: npt=");

    rangend = strstr(range, "-" END_LINE);
    if(!rangend || rangend - range >= sizeof(tempbuf))
        return;
    memcpy(tempbuf, range, rangend - range);
    rtspcontrol->range = osip_strdup(tempbuf);


}

void mk_siprtsp_to_str(mk_sip_rtsp *rtspcontrol, char *rtspbuf)
{
    char pbuf[1024] = { 0 };
    char tempbuf[100] = { 0 };
    snprintf(pbuf, sizeof(pbuf), "%s MANSRTSP/%s" END_LINE "CSeq: 5" END_LINE,
        rtspcontrol->method, rtspcontrol->version);

    if (rtspcontrol->scale)
    {
        snprintf(tempbuf, 100, "Scale: %s" END_LINE, rtspcontrol->scale);
        strcat(pbuf, tempbuf);
    }
    if (rtspcontrol->range)
    {
        memset(tempbuf, 0, sizeof(tempbuf));
        snprintf(tempbuf, 100, "%s" END_LINE, rtspcontrol->range);
        strcat(pbuf, tempbuf);
    }
    strcat(pbuf, END_LINE);
    strcpy(rtspbuf, pbuf);
}

const char *mk_get_siprtsp_method(mk_sip_rtsp *rtspcontrol)
{
    return rtspcontrol->method;
}

const char *mk_get_siprtsp_version(mk_sip_rtsp *rtspcontrol)
{
    return rtspcontrol->version;
}

const char *mk_get_siprtsp_scale(mk_sip_rtsp *rtspcontrol)
{
    return rtspcontrol->scale;
}

const char *mk_get_siprtsp_range(mk_sip_rtsp *rtspcontrol)
{
    return rtspcontrol->range;
}

void mk_set_siprtsp_method(char *method, mk_sip_rtsp *rtspcontrol)
{
    rtspcontrol->method = osip_strdup(method);
}

void mk_set_siprtsp_version(char *version, mk_sip_rtsp *rtspcontrol)
{
    rtspcontrol->version = osip_strdup(version);
}

void mk_set_siprtsp_scale(char *scale, mk_sip_rtsp *rtspcontrol)
{
    rtspcontrol->scale = osip_strdup(scale);
}

void mk_set_siprtsp_range(char *range, mk_sip_rtsp *rtspcontrol)
{
    rtspcontrol->range = osip_strdup(range);
}

